<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class ActivityGoods extends Model
{
    //

    public function getCommonCommissionRateAttr($commonCommissionRate){
        return round($commonCommissionRate,2);
    }

    public function getCommissionRateAttr($commissionRate){
        return round($commissionRate,2);
    }

    public function getServiceRateAttr($serviceRate){
        return round($serviceRate,2);
    }

//    public function getPromotionPriceAttr($promotionPrice){
//        $promotionPrice = $promotionPrice > 0 ? $promotionPrice / 100 : 0;
//        return round($promotionPrice,2);
//    }

    public function getProductPriceAttr($product_price){
        $price = $product_price > 0 ? $product_price / 100 : 0;
        return round($price,2);
    }

    public function activity(){
        return $this->belongsTo(Activity::class,'buyin_activity_id','buyin_activity_id');
    }

    public static function getDetail($id){
        //todo 缺少关联主播、关联直播
        $res = self::find($id);
        $info = [];
        if($res){
            $info = $res->toArray();
            $uid = 0;
            $token = headerToken();
            if($token){
                $user = decode_token($token);
                $uid = $user->id;
            }
            if(intval($uid) >0){
                $r = UserCollection::where(['user_id'=>$uid, 'activity_goods_id'=>$info['id']])->find();
                if($r){
                    $info['is_collection'] = 1;
                }else{
                    $info['is_collection'] = 0;
                }
            }else{
                $info['is_collection'] = 0;
            }
            if($res['extension'] != ''){
                $res['extension'] = json_decode($res['extension']);
            }
        }
        return $res ? $info : [];
    }
}
